export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

// 根据生日计算年龄
export const calculateAge = (birthday: any) => {
  const today = new Date();
  const birthDate = new Date(birthday);  // 将生日字符串转换为Date对象

  let age = today.getFullYear() - birthDate.getFullYear();  // 计算年份差

  // 检查是否已经过了生日
  const todayMonth = today.getMonth();
  const birthMonth = birthDate.getMonth();
  const todayDay = today.getDate();
  const birthDay = birthDate.getDate();

  if (todayMonth < birthMonth || (todayMonth === birthMonth && todayDay < birthDay)) {
    age--;  // 如果还没过生日，年龄减一
  }

  return age;
}